package com.example.sellersale.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sellersale.entity.Goods;
import com.example.sellersale.entity.Order;
import com.example.sellersale.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
@Transactional(rollbackFor = Exception.class)
public interface OrderMapper extends BaseMapper<Order> {
    @Select("select * from sys_order where userId not in (select id from sys_user where !status) ")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "goodsId",property = "goodsId"),
            @Result(column = "userId",property = "userId"),
            //计算后所得
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderDate",property = "orderDate"),
            //计算后所得
            @Result(column = "price",property = "price"),
            @Result(column = "delivery",property = "delivery"),
            //肯定是出货的订单
            @Result(column = "orderType",property = "orderType"),
            @Result(column = "userId",property = "user",javaType = User.class,
                    one = @One(select = "com.example.sellersale.mapper.UserMapper.selectById")
           ),
            @Result(column = "goodsId",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.example.sellersale.mapper.GoodsMapper.selectById")
            )
    })
    List<Order> selectList();

    @Select("select * from sys_order where userId = #{id}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "goodsId",property = "goodsId"),
            @Result(column = "userId",property = "userId"),
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderDate",property = "orderDate"),
            @Result(column = "price",property = "price"),
            @Result(column = "delivery",property = "delivery"),
            @Result(column = "userId",property = "user",javaType = User.class,
                    one = @One(select = "com.example.sellersale.mapper.UserMapper.selectById")
            ),
            @Result(column = "goodsId",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.example.sellersale.mapper.GoodsMapper.selectById")
            )
    })
    List<Order> selectOneOrder(String id);



    @Update("update sys_order set status=#{status} where id=#{id}")
    int updateStatus(Boolean status,String id);

    @Update("update sys_order set delivery=#{status} where id=#{oId} and goodsId=#{gId}")
    int update(String oId,String gId,String status);

    @Override
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    int update(Order entity, Wrapper<Order> updateWrapper);

    //查看月记录
    @Select("select * from sys_order where userId not in (select id from sys_user where !status) and orderDate like #{time}")
    @Results({
            @Result(column = "id",property = "id"),
            @Result(column = "goodsId",property = "goodsId"),
            @Result(column = "userId",property = "userId"),
            //计算后所得
            @Result(column = "orderNum",property = "orderNum"),
            @Result(column = "orderDate",property = "orderDate"),
            //计算后所得
            @Result(column = "price",property = "price"),
            @Result(column = "delivery",property = "delivery"),
            //肯定是出货的订单
            @Result(column = "orderType",property = "orderType"),
            @Result(column = "userId",property = "user",javaType = User.class,
                    one = @One(select = "com.example.sellersale.mapper.UserMapper.selectById")
            ),
            @Result(column = "goodsId",property = "goods",javaType = Goods.class,
                    one = @One(select = "com.example.sellersale.mapper.GoodsMapper.selectById")
            )
    })
    List<Order> speciallyTimeOrder(String time);
}
